package HW2_3.Firefox_page;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import HW2_3.pages.InternalPage;
import HW2_3.pages.LoginPage;
import HW2_3.pages.UserPage;


public class a_login_password {
	private WebDriver driver;
	private WebDriverWait wait;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	private LoginPage loginPage;
	private UserPage userPage;
	private InternalPage internalPage;

	

	
	
	@Before
	public void setUp() throws Exception {
		
		 ProfilesIni allProfiles = new ProfilesIni(); 
	     FirefoxProfile profile = allProfiles.getProfile("default"); 
	     driver = new FirefoxDriver(profile); 
	     
	    baseUrl = "http://localhost/tools/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	
	
	@Test
	public void test_login() throws Exception {
		GoToMainPage();
				assertTrue("Not Main Page", loginPage.isOnMainPage());
		loginAs("admin", "admin");
				assertTrue("LoginAs error", internalPage.isLoginOk());
		LogOut();
			assertTrue("Not logout", loginPage.isOnMainPage());
	}
	
	
	
	@Test
	public void testEmailChange() {
		GoToMainPage();
		 		assertTrue("Not Main Page", loginPage.isOnMainPage());
		loginAs("admin", "admin");
				assertTrue("LoginAs error", internalPage.isLoginOk());
		goToUserProfile();
		changeEmailTo ("admin@admin.ru");
				assertTrue("Not change password",userPage.IsOnManagerPage()); 
		goToUserProfile();
		changeEmailTo ("admin@admin.com");
				assertTrue("Not change password",userPage.IsOnManagerPage()); 
		LogOut();
			assertTrue("Not logout", loginPage.isOnMainPage());
		}
	
	
	private void LogOut() {
		internalPage.menuLogoutLink.click();
		driver.switchTo().alert().accept();
	}
	
	private void changeEmailTo(String email) {
		userPage.emailField.clear();
		userPage.emailField.sendKeys(email);
		userPage.saveButton.click();
	}

	private void goToUserProfile() {
		internalPage.menuProfileLink.click();
		}

	private void loginAs(String userName, String password) {
		loginPage.userNameField.click();
		loginPage.userNameField.sendKeys(userName);
		loginPage.passwordField.click();
		loginPage.passwordField.sendKeys(userName);
		loginPage.loginButton.click();
	}
	
	private void GoToMainPage() {
		driver.get(baseUrl + "PHP4DVD");
		loginPage = PageFactory.initElements(driver, LoginPage.class);
		userPage=PageFactory.initElements(driver, UserPage.class);
		internalPage = PageFactory.initElements(driver, InternalPage.class);
	}

	
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
